<?php

namespace App\Http\Controllers\Backend\Demand;

use App\Http\Controllers\Controller;
use App\Http\Repositories\Demand\DemandRepo;
use App\Http\Requests\Validators\Backend\Demand\DemandValidator;
use App\Traits\HelpFunctions;
use Illuminate\Http\Request;

/**
 * 需求相关
 */
class DemandController extends Controller
{
    use HelpFunctions;

    const VALIDATOR = DemandValidator::class;

    public function __construct()
    {
        $this->self_repo = new DemandRepo;
    }

    public function search(Request $request)
    {
        //参数验证
        $args = $request->only(
            'title',
            'province_id',
            'city_id',
            'area_id',
            'begin_budget',
            'end_budget',
            'introduction',
            'negotiate',
            'begin_deadline',
            'end_deadline',
            'sn',
            'user_id',
            'begin_time',
            'end_time',
            'page',
            "page_num",
        );

        $this->my_validate($args, 'searchRules');

        return $this->self_repo->search($args);
    }

    public function store(Request $request)
    {
        //参数验证
        $args = $request->only(
            'title',
            'province_id',
            'city_id',
            'area_id',
            'budget',
            'introduction',
            'negotiate',
            'deadline',
            'types',
            'resources',
        );

        $this->my_validate($args, 'storeRules');

        $args['user_id'] = $this->getUserId();
        $args['created_at'] = date('Y-m-d H:i:s');
        $args['sn'] = $this->uSN('DM', $args['created_at']);

        //添加数据
        return $this->returnResponse($this->self_repo->createOne($args));
    }

    public function delete($id)
    {
        return $this->self_repo->deleteById($id);
    }

    public function edit(Request $request, $id)
    {
        //参数验证
        $args = $request->only(
            'title',
            'province_id',
            'city_id',
            'area_id',
            'budget',
            'introduction',
            'negotiate',
            'deadline',
            'status'
        );
        $this->my_validate($args, 'editRules');

        //根据id修改数据
        return $this->returnResponse($this->self_repo->editById($id, $args));
    }
}
